#ifdef UTILITY_DISABLE_HEADONLY
#   include "../acronym_for_pinyin.h"
#endif

namespace util   {
namespace detail {

    static const char _letter[] = "ABCDEFGHJKLMNOPQRSTWXYZ";
    
    static const int  _pos_value[] = { 
        1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 
        3722, 3730, 3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249 };

    static const char _second_table[] = 
        "CJWGNSPGCGNE[Y[BTYYZDXYKYGT[JNNJQMBSGZSCYJSYY[PGKBZGY[YWJKGKLJYWKPJQHY[W[DZLSGM"
        "RYPYWWCCKZNKYYGTTNJJNYKKZYTCJNMCYLQLYPYQFQRPZSLWBTGKJFYXJWZLTBNCXJJJJTXDTTSQZYC"
        "DXXHGCK[PHFFSS[YBGXLPPBYLL[HLXS[ZM[JHSOJNGHDZQYKLGJHSGQZHXQGKEZZWYSCSCJXYEYXADZ"
        "PMDSSMZJZQJYZC[J[WQJBYZPXGZNZCPWHKXHQKMWFBPBYDTJZZKQHYLYGXFPTYJYYZPSZLFCHMQSHGM"
        "XXSXJ[[DCSBBQBEFSJYHXWGZKPYLQBGLDLCCTNMAYDDKSSNGYCSGXLYZAYBNPTSDKDYLHGYMYLCXPY["
        "JNDQJWXQXFYYFJLEJPZRXCCQWQQSBNKYMGPLBMJRQCFLNYMYQMSQYRBCJTHZTQFRXQHXMJJCJLXQGJM"
        "SHZKBSWYEMYLTXFSYDSWLYCJQXSJNQBSCTYHBFTDCYZDJWYGHQFRXWCKQKXEBPTLPXJZSRMEBWHJLBJ"
        "SLYYSMDXLCLQKXLHXJRZJMFQHXHWYWSBHTRXXGLHQHFNM[YKLDYXZPYLGG[MTCFPAJJZYLJTYANJGBJ"
        "PLQGDZYQYAXBKYSECJSZNSLYZHSXLZCGHPXZHZNYTDSBCJKDLZAYFMYDLEBBGQYZKXGLDNDNYSKJSHD"
        "LYXBCGHXYPKDJMMZNGMMCLGWZSZXZJFZNMLZZTHCSYDBDLLS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JCDGZYGCTXMZYSCTLKPHTXHTLBJXJLXSCDQXCBBTJFQZFS"
        "LTJBTKQBXXJJLJCHCZDBZJDCZJDCPRNPQCJPFCZLCLZXZDMXMPHJSGZGSZZQLYLWTJPFSYASMCJBTZK"
        "YCWMYTCSJJLJCQLWZMALBXYFBPNLSFHTGJWEJJXXGLLJSTGSHJQLZFKCGNNNSZFDEQFHBSAQTGYLBXM"
        "MYGSZLDYDQMJJRGBJTKGDHGKBLQKBDMBYLXWCXYTTYBKMRTJZXQJBHLMHMJJZMQASLDCYXYQDLQCAFY"
        "WYXQHZ";

} //detail

std::string get_acronym(const std::string& gbk)
{
    std::string result;
    for(int i = 0; i < int(gbk.length()); ++i)
    {
        int h8bit = (unsigned char)(gbk[i]);
        int l8bit = (unsigned char)(gbk[i+1]);
        int width = 0;

        if(h8bit < 0xa1 || l8bit < 0xa1)
        {
            result.append(1, gbk[i]);
            continue;
        }
        else
        {
            width = (h8bit - 160) * 100 + l8bit - 160;
        }

        if(width > 1600 && width < 5590)
        {
            for(int j = 22; j >= 0; --j)
            {
                if(width >= detail::_pos_value[j])
                {
                    result.append(1, detail::_letter[j]);
                    i++;
                    break;
                }
            }
            continue;
        }
        else
        {
            i++;
            width = ( h8bit - 160 - 56 ) * 94 + l8bit - 161;
            if(width >= 0 && width <= 3007)
                result.append(1, detail::_second_table[width]);
            else
            {
                result.append(1, (char)h8bit);
                result.append(1, (char)l8bit);
            }
        }
    }

    return result;
}

} // util